using System;
using System.Collections.Generic;
using UD.Seguridad.Entity;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using System.Data;
using UD.Utilities.Exceptions;

namespace UD.Seguridad.DataAccess
{
    public class ProgramaDAL : GeneralDAL
    {
        public ProgramaDAL()
        {
        }
        public int InsertarPrograma(Programa pPrograma)
        {
            try
            {
                Database vDataBase = ObtenerInstancia();
                using (DbCommand vDbCommand = vDataBase.GetStoredProcCommand("usp_SYSEXTUD_SEG_Programa_Insertar"))
                {
                    int vResultado;
                    vDataBase.AddOutParameter(vDbCommand, "@IdPrograma", DbType.Int32, 18);
                    vDataBase.AddInParameter(vDbCommand, "@IdModulo", DbType.Int32, pPrograma.IdModulo);
                    vDataBase.AddInParameter(vDbCommand, "@NombrePrograma", DbType.String, pPrograma.NombrePrograma);
                    vDataBase.AddInParameter(vDbCommand, "@CodigoPrograma", DbType.String, pPrograma.CodigoPrograma);
                    vDataBase.AddInParameter(vDbCommand, "@PosicionPrograma", DbType.Int32, pPrograma.PosicionPrograma);
                    vDataBase.AddInParameter(vDbCommand, "@EstadoPrograma", DbType.Boolean, pPrograma.EstadoPrograma);
                    vDataBase.AddInParameter(vDbCommand, "@PadrePrograma", DbType.Int32, pPrograma.PadrePrograma);
                    vDataBase.AddInParameter(vDbCommand, "@UsuarioCrea", DbType.String, pPrograma.UsuarioCrea);
                    vResultado = vDataBase.ExecuteNonQuery(vDbCommand);
                    pPrograma.IdPrograma = Convert.ToInt32(vDataBase.GetParameterValue(vDbCommand, "@IdPrograma").ToString());
                    GenerarLogAuditoria(pPrograma, vDbCommand);
                    return vResultado;
                }
            }
            catch (UserInterfaceException)
            {
                throw;
            }
            catch (Exception ex)
            {
                 throw new GenericException(ex);
            }
        }
        public int ModificarPrograma(Programa pPrograma)
        {
            try
            {
                Database vDataBase = ObtenerInstancia();
                using (DbCommand vDbCommand = vDataBase.GetStoredProcCommand("usp_SYSEXTUD_SEG_Programa_Modificar"))
                {
                    int vResultado;
                    vDataBase.AddInParameter(vDbCommand, "@IdPrograma", DbType.Int32, pPrograma.IdPrograma);
                    vDataBase.AddInParameter(vDbCommand, "@IdModulo", DbType.Int32, pPrograma.IdModulo);
                    vDataBase.AddInParameter(vDbCommand, "@NombrePrograma", DbType.String, pPrograma.NombrePrograma);
                    vDataBase.AddInParameter(vDbCommand, "@CodigoPrograma", DbType.String, pPrograma.CodigoPrograma);
                    vDataBase.AddInParameter(vDbCommand, "@PosicionPrograma", DbType.Int32, pPrograma.PosicionPrograma);
                    vDataBase.AddInParameter(vDbCommand, "@EstadoPrograma", DbType.Boolean, pPrograma.EstadoPrograma);
                    vDataBase.AddInParameter(vDbCommand, "@PadrePrograma", DbType.Int32, pPrograma.PadrePrograma);
                    vDataBase.AddInParameter(vDbCommand, "@UsuarioModifica", DbType.String, pPrograma.UsuarioModifica);
                    vResultado = vDataBase.ExecuteNonQuery(vDbCommand);
                    GenerarLogAuditoria(pPrograma, vDbCommand);
                    return vResultado;
                }
            }
            catch (UserInterfaceException)
            {
                throw;
            }
            catch (Exception ex)
            {
                 throw new GenericException(ex);
            }
        }
        public int EliminarPrograma(Programa pPrograma)
        {
            try
            {
                Database vDataBase = ObtenerInstancia();
                using (DbCommand vDbCommand = vDataBase.GetStoredProcCommand("usp_SYSEXTUD_SEG_Programa_Eliminar"))
                {
                    int vResultado;
                    vDataBase.AddInParameter(vDbCommand, "@IdPrograma", DbType.Int32, pPrograma.IdPrograma);
                    vResultado = vDataBase.ExecuteNonQuery(vDbCommand);
                    GenerarLogAuditoria(pPrograma, vDbCommand);
                    return vResultado;
                }
            }
            catch (UserInterfaceException)
            {
                throw;
            }
            catch (Exception ex)
            {
                 throw new GenericException(ex);
            }
        }


        public Programa ConsultarPrograma(int pIdPrograma)
        {
            try
            {
                Database vDataBase = ObtenerInstancia();
                using (DbCommand vDbCommand = vDataBase.GetStoredProcCommand("usp_SYSEXTUD_SEG_Programa_Consultar"))
                {
                    vDataBase.AddInParameter(vDbCommand, "@IdPrograma", DbType.Int32, pIdPrograma);
                    using (IDataReader vDataReaderResults = vDataBase.ExecuteReader(vDbCommand))
                    {
                        Programa vPrograma = new Programa();
                        while (vDataReaderResults.Read())
                        {
                            vPrograma.IdPrograma = vDataReaderResults["IdPrograma"] != DBNull.Value ? Convert.ToInt32(vDataReaderResults["IdPrograma"].ToString()) : vPrograma.IdPrograma;
                            vPrograma.IdModulo = vDataReaderResults["IdModulo"] != DBNull.Value ? Convert.ToInt32(vDataReaderResults["IdModulo"].ToString()) : vPrograma.IdModulo;
                            vPrograma.NombrePrograma = vDataReaderResults["NombrePrograma"] != DBNull.Value ? Convert.ToString(vDataReaderResults["NombrePrograma"].ToString()) : vPrograma.NombrePrograma;
                            vPrograma.CodigoPrograma = vDataReaderResults["CodigoPrograma"] != DBNull.Value ? Convert.ToString(vDataReaderResults["CodigoPrograma"].ToString()) : vPrograma.CodigoPrograma;
                            vPrograma.PosicionPrograma = vDataReaderResults["PosicionPrograma"] != DBNull.Value ? Convert.ToInt32(vDataReaderResults["PosicionPrograma"].ToString()) : vPrograma.PosicionPrograma;
                            vPrograma.EstadoPrograma = vDataReaderResults["EstadoPrograma"] != DBNull.Value ? Convert.ToBoolean(vDataReaderResults["EstadoPrograma"].ToString()) : vPrograma.EstadoPrograma;
                            vPrograma.PadrePrograma = vDataReaderResults["PadrePrograma"] != DBNull.Value ? Convert.ToInt32(vDataReaderResults["PadrePrograma"].ToString()) : vPrograma.PadrePrograma;
                            vPrograma.UsuarioCrea = vDataReaderResults["UsuarioCrea"] != DBNull.Value ? Convert.ToString(vDataReaderResults["UsuarioCrea"].ToString()) : vPrograma.UsuarioCrea;
                            vPrograma.FechaCrea = vDataReaderResults["FechaCrea"] != DBNull.Value ? Convert.ToDateTime(vDataReaderResults["FechaCrea"].ToString()) : vPrograma.FechaCrea;
                            vPrograma.UsuarioModifica = vDataReaderResults["UsuarioModifica"] != DBNull.Value ? Convert.ToString(vDataReaderResults["UsuarioModifica"].ToString()) : vPrograma.UsuarioModifica;
                            vPrograma.FechaModifica = vDataReaderResults["FechaModifica"] != DBNull.Value ? Convert.ToDateTime(vDataReaderResults["FechaModifica"].ToString()) : vPrograma.FechaModifica;
                        }
                        return vPrograma;
                    }
                }
            }
            catch (UserInterfaceException)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw new GenericException(ex);
            }
        }

        public List<Programa> ConsultarProgramas(int? pIdModulo, String pNombrePrograma, String pCodigoPrograma, int? pPosicionPrograma, Boolean? pEstadoPrograma, int? pPadrePrograma)
        {
            try
            {
                Database vDataBase = ObtenerInstancia();
                using (DbCommand vDbCommand = vDataBase.GetStoredProcCommand("usp_SYSEXTUD_SEG_Programas_Consultar"))
                {
                    if(pIdModulo != null)
                         vDataBase.AddInParameter(vDbCommand, "@IdModulo", DbType.Int32, pIdModulo);
                    if(pNombrePrograma != null)
                         vDataBase.AddInParameter(vDbCommand, "@NombrePrograma", DbType.String, pNombrePrograma);
                    if(pCodigoPrograma != null)
                         vDataBase.AddInParameter(vDbCommand, "@CodigoPrograma", DbType.String, pCodigoPrograma);
                    if(pPosicionPrograma != null)
                         vDataBase.AddInParameter(vDbCommand, "@PosicionPrograma", DbType.Int32, pPosicionPrograma);
                    if(pEstadoPrograma != null)
                         vDataBase.AddInParameter(vDbCommand, "@EstadoPrograma", DbType.Boolean, pEstadoPrograma);
                    if(pPadrePrograma != null)
                         vDataBase.AddInParameter(vDbCommand, "@PadrePrograma", DbType.Int32, pPadrePrograma);
                    using (IDataReader vDataReaderResults = vDataBase.ExecuteReader(vDbCommand))
                    {
                        List<Programa> vListaPrograma = new List<Programa>();
                        while (vDataReaderResults.Read())
                        {
                                Programa vPrograma = new Programa();
                            vPrograma.IdPrograma = vDataReaderResults["IdPrograma"] != DBNull.Value ? Convert.ToInt32(vDataReaderResults["IdPrograma"].ToString()) : vPrograma.IdPrograma;
                            vPrograma.IdModulo = vDataReaderResults["IdModulo"] != DBNull.Value ? Convert.ToInt32(vDataReaderResults["IdModulo"].ToString()) : vPrograma.IdModulo;
                            vPrograma.NombrePrograma = vDataReaderResults["NombrePrograma"] != DBNull.Value ? Convert.ToString(vDataReaderResults["NombrePrograma"].ToString()) : vPrograma.NombrePrograma;
                            vPrograma.CodigoPrograma = vDataReaderResults["CodigoPrograma"] != DBNull.Value ? Convert.ToString(vDataReaderResults["CodigoPrograma"].ToString()) : vPrograma.CodigoPrograma;
                            vPrograma.PosicionPrograma = vDataReaderResults["PosicionPrograma"] != DBNull.Value ? Convert.ToInt32(vDataReaderResults["PosicionPrograma"].ToString()) : vPrograma.PosicionPrograma;
                            vPrograma.EstadoPrograma = vDataReaderResults["EstadoPrograma"] != DBNull.Value ? Convert.ToBoolean(vDataReaderResults["EstadoPrograma"].ToString()) : vPrograma.EstadoPrograma;
                            vPrograma.PadrePrograma = vDataReaderResults["PadrePrograma"] != DBNull.Value ? Convert.ToInt32(vDataReaderResults["PadrePrograma"].ToString()) : vPrograma.PadrePrograma;
                            vPrograma.UsuarioCrea = vDataReaderResults["UsuarioCrea"] != DBNull.Value ? Convert.ToString(vDataReaderResults["UsuarioCrea"].ToString()) : vPrograma.UsuarioCrea;
                            vPrograma.FechaCrea = vDataReaderResults["FechaCrea"] != DBNull.Value ? Convert.ToDateTime(vDataReaderResults["FechaCrea"].ToString()) : vPrograma.FechaCrea;
                            vPrograma.UsuarioModifica = vDataReaderResults["UsuarioModifica"] != DBNull.Value ? Convert.ToString(vDataReaderResults["UsuarioModifica"].ToString()) : vPrograma.UsuarioModifica;
                            vPrograma.FechaModifica = vDataReaderResults["FechaModifica"] != DBNull.Value ? Convert.ToDateTime(vDataReaderResults["FechaModifica"].ToString()) : vPrograma.FechaModifica;
                                vListaPrograma.Add(vPrograma);
                        }
                        return vListaPrograma;
                    }
                }
            }
            catch (UserInterfaceException)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw new GenericException(ex);
            }
        }
    }
}
